# -*- coding: UTF-8 -*-
import urllib3,re,sys,urllib.request
import pandas as pd
def GetSinaData(stock_code):
    if stock_code[0] == '6':
      url='http://hq.sinajs.cn/list=sh'+stock_code
    else:
      url='http://hq.sinajs.cn/list=sz'+stock_code
    # page = urllib3.urlopen(url)
    # print(url)
    page = urllib.request.urlopen(url)
    html = page.read()
    html = html.decode("gb2312")
    # print(html)
    data = re.compile(r'="(.*?)";')
    datalist = re.findall(data,html)
    all_item = datalist[0].split(',')
    print(all_item)
    # stock_name_orig = all_item[0].decode("GB2312")
    # stock_name_final = stock_name_orig.encode("UTF-8")

    stock_dict = {}
    stock_dict['日期'] = all_item[30]
    stock_dict['时间'] = all_item[31]
    stock_dict['状态'] = all_item[32]
    stock_dict['股票名称'] = all_item[0] #stock_name_final
    stock_dict['开盘价'] = all_item[1]
    stock_dict['昨日收盘'] = all_item[2]
    stock_dict['当前价格'] = all_item[3]
    stock_dict['最高价格'] = all_item[4]
    stock_dict['最低价格'] = all_item[5]
    stock_dict['买入价'] = all_item[6]
    stock_dict['卖出价'] = all_item[7]
    stock_dict['成交数量'] = all_item[8]
    stock_dict['成交金额'] = all_item[9]
    stock_dict['买1量'] = all_item[10]
    stock_dict['买1价'] = all_item[11]
    stock_dict['买2量'] = all_item[12]
    stock_dict['买2价'] = all_item[13]
    stock_dict['买3量'] = all_item[14]
    stock_dict['买3价'] = all_item[15]
    stock_dict['买4量'] = all_item[16]
    stock_dict['买4价'] = all_item[17]
    stock_dict['买5量'] = all_item[18]
    stock_dict['买5价'] = all_item[19]
    stock_dict['卖1量'] = all_item[20]
    stock_dict['卖1价'] = all_item[21]
    stock_dict['卖2量'] = all_item[22]
    stock_dict['卖2价'] = all_item[23]
    stock_dict['卖3量'] = all_item[24]
    stock_dict['卖3价'] = all_item[25]
    stock_dict['卖4量'] = all_item[26]
    stock_dict['卖4价'] = all_item[27]
    stock_dict['卖5量'] = all_item[28]
    stock_dict['卖5价'] = all_item[29]


    # for item in all_item:
    #


    #    print(item)

    return stock_dict

def GetSinaData_pd(ts_code):
   dict= GetSinaData(ts_code)
   print("dict\n",dict)
   return pd.DataFrame([dict])
stock_dict = GetSinaData("000519")

print('==========================================================')
if stock_dict['状态']=="00":
    zt='正常'
else:
    zt='停牌'
print( '日期：' + stock_dict['日期'] + '   时间：' + stock_dict['时间'] + '   状态：' +zt)
print( '股票名称：' + stock_dict['股票名称'])
print( '开盘价：' + stock_dict['开盘价'])
print( '昨日收盘：' + stock_dict['昨日收盘'])
print( '最高价格：' + stock_dict['最高价格'])
print( '最低价格：' + stock_dict['最低价格'])
print( '买入价：' + stock_dict['买入价'])
print( '卖出价：' + stock_dict['卖出价'])
print( '成交数量：' + stock_dict['成交数量'])
print( '成交金额：' + stock_dict['成交金额'])
print( '----------------------------------------------------------')
# print '-->买5价格：' + stock_dict['买5价'] + '   数量：' + stock_dict['买5量']
# print '-->买4价格：' + stock_dict['买4价'] + '   数量：' + stock_dict['买4量']
# print '-->买3价格：' + stock_dict['买3价'] + '   数量：' + stock_dict['买3量']
# print '-->买2价格：' + stock_dict['买2价'] + '   数量：' + stock_dict['买2量']
# print '-->买1价格：' + stock_dict['买1价'] + '   数量：' + stock_dict['买1量']
# print '  当前价格：' + stock_dict['当前价格']
# print '<--卖1价格：' + stock_dict['卖1价'] + '   数量：' + stock_dict['卖1量']
# print '<--卖2价格：' + stock_dict['卖2价'] + '   数量：' + stock_dict['卖2量']
# print '<--卖3价格：' + stock_dict['卖3价'] + '   数量：' + stock_dict['卖3量']
# print '<--卖4价格：' + stock_dict['卖4价'] + '   数量：' + stock_dict['卖4量']
# print '<--卖5价格：' + stock_dict['卖5价'] + '   数量：' + stock_dict['卖5量']
# print '=========================================================='
df=GetSinaData_pd("000519")
# df=GetSinaData_pd("000519")
print(df)